package com.example.app_dynamic_icon

import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** AppDynamicIconPlugin */
class AppDynamicIconPlugin : FlutterPlugin, MethodCallHandler {

    private lateinit var channel: MethodChannel
    private lateinit  var applicationContext: Context

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "app_dynamic_icon")
        applicationContext = flutterPluginBinding.applicationContext
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        when (call.method) {
            "getPlatformVersion" -> {
                result.success("Android ${android.os.Build.VERSION.RELEASE}")
            }
            "changeIcon" -> {
                changeIcon(true)
                result.success("true")
            }
            "resetIcon" -> {
                changeIcon(false)
                result.success("true")
            }
            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

    /**
     * 是否使用备用icon
     */
    private fun changeIcon(enableBackUp: Boolean) {
        val pm = applicationContext?.packageManager
        if (enableBackUp) {
            pm?.setComponentEnabledSetting(
                ComponentName(applicationContext!!, "com.example.app_dynamic_icon_example.MainActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )

            //显示TextActivity图标
            pm?.setComponentEnabledSetting(
                ComponentName(applicationContext!!, "com.example.app_dynamic_icon_example.DynamicIconActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP
            )
        } else {
            //显示TextActivity图标
            pm?.setComponentEnabledSetting(
                ComponentName(applicationContext!!, "com.example.app_dynamic_icon_example.DynamicIconActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )

            pm?.setComponentEnabledSetting(
                ComponentName(applicationContext!!, "com.example.app_dynamic_icon_example.MainActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP
            )
        }
    }
}
